import {
    getApiMajorList,
    getRestfulMallLevels,
    getRestfulMallMemberGroups,
    getRestfulMallV2CouponDetail,
    getRestfulPointsFilters,
    getRestfulV2MallCoupons,
    postRestV3WechatShortUrl
} from "@/assets/api/index";
import { ElMessage } from "element-plus";
import { TypeFilters } from "@/interface/filters";
import { copyText } from "@/utils";
import { getCouponValidPeriodDetail } from "@/components/formUtils/ValidPeriod";

// 优惠券列表
export const getRestfulV2MallCouponsHandler = (params: ObjValueAny = {}, config: ObjValueAny = {}, header: ObjValueAny = {}): Promise<ObjValueAny> => {
    return new Promise(resolve => {
        getRestfulV2MallCoupons(params, config, header).then((resp: ObjValueAny) => {
            if (resp.code === 200) {
                for (let row of resp.data) {
                    row.validPeriodText = getCouponValidPeriodDetail(row);
                }
            }
            resolve(resp);
        });
    })
}

// 优惠券详情
export const getRestfulMallV2CouponDetailHandler = (id: number, params: ObjValueAny = {}, config: ObjValueAny = {}, header: ObjValueAny = {}): Promise<ObjValueAny> => {
    return new Promise(resolve => {
        getRestfulMallV2CouponDetail(id, params, config, header).then((resp: ObjValueAny) => {
            if (resp.code === 200) {
                resp.data.validPeriodText = getCouponValidPeriodDetail(resp.data);
            }
            resolve(resp);
        });
    })
}

// 店铺列表 用于选择项或筛选
export const getApiMajorListFilter = (isMall: boolean = true): Promise<TypeFilters.filterApi[]> => {
    return new Promise(resolve => {
        getApiMajorList({ page_size: 10000 }).then((resp: ObjValueAny) => {
            if (resp.code === 200) {
                if (isMall) {
                    resolve([{ id: 1, name: '商场' }].concat(resp.data));
                } else {
                    resolve(resp.data);
                }
            } else {
                resolve([]);
                ElMessage.error(resp.msg);
            }
        });
    })
}

// 积分变动来源 用于选择项或筛选
export const getRestfulPointsFiltersHandler = (): Promise<ObjValueAny[]> => {
    return new Promise(async resolve => {
        getRestfulPointsFilters().then((resp: ObjValueAny) => {
            const data: ObjValueAny[] = [];
            if (resp.code === 200) {
                for (let val of resp.data) {
                    const label = val.replace('第三方同步', '').trim();
                    const value = label.replace('*', '第三方同步-');
                    data.push({ value, label })
                }
                resolve(data);
            } else {
                ElMessage.error(resp.msg);
                resolve([]);
            }
        })
    })
}

// 会员等级 用于选择项或筛选
export const getRestfulMallLevelsFilter = (): Promise<TypeFilters.filterApi[]> => {
    return new Promise(resolve => {
        getRestfulMallLevels({
            page: 1,
            per_page: 1000
        }).then((resp: { code: number, msg: string, data: TypeFilters.filterApi[] }) => {
            if (resp.code === 200) {
                resolve(resp.data || []);
            } else {
                resolve([]);
                ElMessage.error(resp.msg);
            }
        })
    })
}

// 人群包 用于选择项或筛选
export const getRestfulMallMemberGroupsFilter = (): Promise<TypeFilters.filterApi[]> => {
    return new Promise(resolve => {
        getRestfulMallMemberGroups({
            page: 1,
            per_page: 10000,
            status: 1,
        }, { noRepeat: 3 }).then((resp: { code: number, msg: string, data: TypeFilters.filterApi[] }) => {
            if (resp.code === 200) {
                resolve(resp.data || []);
            } else {
                resolve([]);
                ElMessage.error(resp.msg);
            }
        })
    })
}

// 复制微信外推广链接
export const outsideCloneLinkApi = (params: ObjValueAny) => {
    ElMessage.closeAll();
    ElMessage({
        showClose: true,
        message: '复制中...',
        type: 'success',
        duration: 0,
    });
    postRestV3WechatShortUrl(params, { noRepeat: 1 }).then((resp: ObjValueAny) => {
        ElMessage.closeAll();
        if (resp.code === 200) {
            copyText(resp.data);
        } else {
            ElMessage.error(resp.msg);
        }
    });
}